﻿using System;
using System.Collections.Generic;
using System.Text;
using DevExpress.Web.ASPxSplitter;
using FeatureCenter.Module.InfoPanels;
using DevExpress.Web.ASPxRoundPanel;
using System.Web.UI;
using DevExpress.ExpressApp.Templates;
using System.Web.UI.WebControls;
using DevExpress.ExpressApp.Web.Templates.ActionContainers;

namespace FeatureCenter.Module.Web.InfoPanels {
    public interface ICustomControlsPlaceHolder : IFrameTemplate {
        Control PlaceHolder { get; }
    }
    public class InfoPanelsViewController : InfoPanelsViewControllerBase<ICustomControlsPlaceHolder> {
        private const string TextPanelID = "TP";
        private const string ActionContainersPanelID = "ACP";
        private const string RelatedViewPanelID = "RVP";
        private ASPxRoundPanel textPanel;
        private ASPxRoundPanel actionContainersPanel;
        private ASPxRoundPanel relatedViewPanel;
        private LiteralControl literal;
        protected override void SetInfoTextToControls(string text) {
            if(literal != null) {
                literal.Text = text.Replace(Environment.NewLine, "<br/>");
            }
        }
        protected override void AddControlsToTemplateCore(ICustomControlsPlaceHolder template) {
            //template.PlaceHolder.Paddings.Padding = Unit.Pixel(10);
            Control templatePlaceHolder = template.PlaceHolder;
            if(templatePlaceHolder != null) {
                textPanel = CreateRoundPanel(Hints.FrameTemplates_InfoPanels_ContextualInfo, TextPanelID);
                literal = new LiteralControl();
                textPanel.Controls.Add(literal);
                templatePlaceHolder.Controls.Add(textPanel);

                actionContainersPanel = CreateRoundPanel(Hints.FrameTemplates_InfoPanels_ContextualActions, ActionContainersPanelID);
                ActionContainerHolder holder = new ActionContainerHolder();
                holder.ID = "ContextHolder";
                holder.Categories = "ContextActions";
                actionContainersPanel.Controls.Add(holder);
                templatePlaceHolder.Controls.Add(actionContainersPanel);

                relatedViewPanel = CreateRoundPanel(Hints.FrameTemplates_InfoPanels_ContextualView, RelatedViewPanelID);
                relatedViewPanel.Controls.Add((Control)CreateRelatedViewControl());
                relatedViewPanel.Visible = View is DevExpress.ExpressApp.DetailView;
                templatePlaceHolder.Controls.Add(relatedViewPanel);
            }
        }

        private ASPxRoundPanel CreateRoundPanel(string caption, string id) {
            ASPxRoundPanel panel = new ASPxRoundPanel();
            panel.ID = id;
            panel.Width = Unit.Percentage(100);
            panel.Style[HtmlTextWriterStyle.MarginBottom] = "5px";
            panel.HeaderText = caption;
            return panel;
        }
        protected override void RemoveControlsFromTemplateCore(ICustomControlsPlaceHolder template) {
            Control templatePlaceHolder = template.PlaceHolder;
            if(templatePlaceHolder != null) {
                templatePlaceHolder.Controls.Remove(textPanel);
                templatePlaceHolder.Controls.Remove(actionContainersPanel);
                templatePlaceHolder.Controls.Remove(relatedViewPanel);
            }
            textPanel = null;
            actionContainersPanel = null;
            literal = null;
        }
        protected override bool NeedRecreateRelatedViewFrameTemplate {
            get { return true; }
        }
    }
}
